#pragma once
#include <vector>
#include <deque>
#include <map>
#include <conio.h>
#include <mutex>
#include "Player.h"
#include "..\Course\Console.h"
#include <thread>


using namespace std;
using namespace Course;
mutex mtx_queue_g;
class Game
{
private:

	vector<vector<int>> matrix;
	deque<Pos> *gQueue;
	deque<char> *pQueue1, *pQueue2;
	int matrixSize;

public:

	Game()
	{
		matrix[0][0];
	}

	Game(int boardsize, deque<Pos> &gQueue,  deque<char> &pQueue1, deque<char> &pQueue2, Player p1, Player p2)
	{
		matrixSize = boardsize;
		matrix = vector<vector<int>>(boardsize);
		for (int i = 0; i < boardsize; i++)
		{
			matrix[i] = vector<int>(boardsize);
		}
		for (int r = 0; r < matrixSize; r++)
		{
			for (int c = 0; c < matrixSize; c++)
			{
					matrix[c][r] = 'x';
			}
		}
		this->pQueue1 = &pQueue1;
		this->pQueue2 = &pQueue2;
		this->gQueue = &gQueue;
		Pos pos1 = p1.GetPos();
		Pos pos2 = p2.GetPos();
		matrix[pos1.xCoord][pos1.yCoord] = pos1.id;
		matrix[pos2.xCoord][pos2.yCoord] = pos2.id;

	}

	void UpdateMatrix()
	{
		Pos position;
		mtx_queue_g.lock();
		if (!gQueue->empty())
		{
			position = gQueue->front();
			gQueue->pop_front();
			for (int r = 0; r < matrixSize; r++)
			{
				for (int c = 0; c < matrixSize; c++)
				{
					if (matrix[c][r] == position.id)
					{
						matrix[c][r] = 'x';
					}
				}
			}
			matrix[position.yCoord][position.xCoord] = position.id;
		}

		mtx_queue_g.unlock();

	}

	void DrawMatrix()
	{
		Console::clrscr();
		//system("CLS");
		for (int r = 0; r < matrixSize; r++)
		{
			for (int c = 0; c < matrixSize; c++)
			{
				cout << (char)matrix[r][c] << "   ";
			}
			cout << endl << endl;
		}
	}

	void PushPlayerQueue(char c)
	{
		mtx_queue_g.lock();
		if (c == 'a' || c == 'w' || c == 's' || c == 'd')
		{
			pQueue1->push_back(c);
		}
		if (c == 'i' || c == 'j' || c == 'k' || c == 'l')
		{
			pQueue2->push_back(c);
		}
		mtx_queue_g.unlock();

	}

	void operator() ()
	{
		char c = 'z';
		if (_kbhit() != 0)
			c = _getch();
		while (c != 'q')
		{
			DrawMatrix();
			PushPlayerQueue(c);
			c = ' ';
			UpdateMatrix();
			this_thread::sleep_for(chrono::milliseconds(100));
			if (_kbhit() != 0)
				c = _getch();
		}
	}

	~Game()
	{
	}
};

